package cn.labbook.bluetooth.shiro;

import cn.labbook.bluetooth.common.SysExceptionEnum;
import cn.labbook.bluetooth.common.TokenManager;
import cn.labbook.bluetooth.constant.AdminConstant;
import cn.labbook.bluetooth.constant.LoginStatusConstant;
import cn.labbook.bluetooth.constant.UserConstant;
import cn.labbook.bluetooth.constant.UserLoginLogConstant;
import cn.labbook.bluetooth.entity.Admin;
import cn.labbook.bluetooth.entity.AdminLoginLog;
import cn.labbook.bluetooth.entity.AdminRes;
import cn.labbook.bluetooth.entity.sql.AdminResourceInfo;
import cn.labbook.bluetooth.exception.SysAuthException;
import cn.labbook.bluetooth.service.IAdminLoginLogService;
import cn.labbook.bluetooth.service.IAdminResService;
import cn.labbook.bluetooth.service.IAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;


/**
 * 权限认证
 */

public class SysAuthenticationRealm extends AuthorizingRealm {
    @Autowired
    private IAdminService adminService;

    @Autowired
    private IAdminLoginLogService adminLoginLogService;

    @Autowired
    private IAdminResService adminResService;

    /**
     * 获取认证信息
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) {
        AdminLoginLog log = new AdminLoginLog();
        try {
            SysToken sysToken = (SysToken) token;

            String userName = sysToken.getUsername();
            String passWord = new String(sysToken.getPassword());
            Admin admin = adminService.login(userName, passWord);


            log.setLoginIp(sysToken.getHost());
            //没错
            log.setLoginName(userName);

            log.setLoginTime(new Date());


            if (admin == null) {
                throw new SysAuthException(SysExceptionEnum.UNKNOWN_ACCOUNT.getMsg());
            } else if (admin.getValid() != AdminConstant.VALID) {

                log.setAdminId(admin.getAdminId());
                log.setAdminName(admin.getAdminName());
                log.setLoginStatus(LoginStatusConstant.FAIL);
                log.setType(UserLoginLogConstant.TYPE.LOCKED_ACCOUNT);
                throw new SysAuthException(SysExceptionEnum.UNKNOWN_ACCOUNT.getMsg());
            } else {
                log.setAdminId(admin.getAdminId());
                log.setAdminName(admin.getAdminName());
                log.setLoginStatus(LoginStatusConstant.SUCCESS);
                log.setType(UserLoginLogConstant.TYPE.SUCCESS);
                // token 对象
                TokenUser tokenUser = new TokenUser();
                //用户信息
                tokenUser.setAdmin(admin);
                return new SimpleAuthenticationInfo(tokenUser, passWord, getName());
            }
        } finally {
//            if (log.canSave) {
            adminLoginLogService.save(log);
        }
    }

    /**
     * 获取权限信息
     *
     * @param principals
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //需要自己实现权限逻辑
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        // token 对象
        TokenUser tokenUser = TokenManager.getToken();
        //用户信息
        String adminId = tokenUser.getAdminId();
        Admin admin = tokenUser.getAdmin();
        if (admin.getAdminStatus() == AdminConstant.position.MANUFACTURERS || admin.getAdminStatus() == AdminConstant.position.LANDLORD){
            info.addStringPermission("manufacturers");
        }else if(admin.getAdminStatus() == AdminConstant.position.ZK) {
            info.addStringPermission("zkis");
        }else if(admin.getAdminStatus() == AdminConstant.position.DEALER) {
            info.addStringPermission("dealer");
        }
        if(admin.getAdminStatus() == AdminConstant.position.DEALER && admin.getPermission() == 1){
            info.addStringPermission("server");
        }
//        List<AdminResourceInfo> adminResourcess = adminResService.findAdminResourcess(adminId);
//
//        for(AdminResourceInfo  adminResourceInfo: adminResourcess){
//            info.addStringPermission(adminResourceInfo.getResourceKey());
//        }

        return info;
    }
}
